package com.jianbing.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Created by Han, Tixiang on 2016/8/1 0001.
 */
public class StringUtils {
    private static final Log LOG = LogFactory.getLog(StringUtils.class);

    public StringUtils() {
    }

    public static Integer subStringToInteger(String src, String start, String to) {
        return stringToInteger(subString(src, start, to));
    }

    public static String subString(String src, String start, String to) {
        int indexFrom = start == null ? 0 : src.indexOf(start);
        int indexTo = to == null ? src.length() : src.indexOf(to);
        if (indexFrom >= 0 && indexTo >= 0 && indexFrom <= indexTo) {
            indexFrom += start.length();
            return src.substring(indexFrom, indexTo);
        } else {
            return null;
        }
    }

    public static Integer stringToInteger(String in) {
        if (in == null) {
            return null;
        } else {
            in = in.trim();
            if (in.length() == 0) {
                return null;
            } else {
                try {
                    return Integer.valueOf(Integer.parseInt(in));
                } catch (NumberFormatException var2) {
                    LOG.warn("stringToInteger fail,string=" + in, var2);
                    return null;
                }
            }
        }
    }

    public static boolean equals(String a, String b) {
        return a == null ? b == null : a.equals(b);
    }

    public static boolean equalsIgnoreCase(String a, String b) {
        return a == null ? b == null : a.equalsIgnoreCase(b);
    }

    public static boolean isEmpty(String value) {
        return value == null || value.length() == 0;
    }

    public static boolean isNotEmpty(String value) {
        return !isEmpty(value);
    }

    public static int lowerHashCode(String text) {
        if (text == null) {
            return 0;
        } else {
            int h = 0;

            for (int i = 0; i < text.length(); ++i) {
                char ch = text.charAt(i);
                if (ch >= 65 && ch <= 90) {
                    ch = (char) (ch + 32);
                }

                h = 31 * h + ch;
            }

            return h;
        }
    }
}